package com.cxb.apiDriver.service;

import com.cxb.apiDriver.remote.ServiceDriverUserClient;
import com.cxb.apiDriver.remote.ServiceVerificationcodeClient;
import com.cxb.internalcommon.constant.CommonStatusEnum;
import com.cxb.internalcommon.constant.DriverCarConstants;
import com.cxb.internalcommon.constant.IdentityConstant;
import com.cxb.internalcommon.constant.TokenConstants;
import com.cxb.internalcommon.dto.ResponseResult;
import com.cxb.internalcommon.request.VerificationCodeDTO;
import com.cxb.internalcommon.responese.DriverUserExistsResponse;
import com.cxb.internalcommon.responese.NumberCodeResponse;
import com.cxb.internalcommon.responese.TokenResponse;
import com.cxb.internalcommon.util.JwtUtils;
import com.cxb.internalcommon.util.RedisPrefixUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VerificationCodeService {

    @Autowired
    ServiceDriverUserClient serviceDriverUserClient;

    @Autowired
    ServiceVerificationcodeClient serviceVerificationcodeClient;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    public ResponseResult  getAndCheckVerificationCode(String driverPhone){

        //查询service-driver-user,该手机号的司机是否存在
        ResponseResult<DriverUserExistsResponse> driverUserExistsResponseResponseResult = serviceDriverUserClient.getUser(driverPhone);
        DriverUserExistsResponse data = driverUserExistsResponseResponseResult.getData();
        int ifExists = data.getIfExists();
        if(ifExists== DriverCarConstants.DRIVER_NOT_EXISTS){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode(),
                    CommonStatusEnum.DRIVER_NOT_EXISTS.getValue());
        }
        log.info(driverPhone+"司机存在");
        //获取验证码
        ResponseResult<NumberCodeResponse> numberCodeResult = serviceVerificationcodeClient.getNumberCode(6);
        NumberCodeResponse numberCodeResponse = numberCodeResult.getData();
        int numberCode = numberCodeResponse.getNumberCode();
        log.info("验证码:"+numberCode);
        //调用第三方发送验证码

        //存入redis
        String key = RedisPrefixUtils.generatorKeyByPhone(driverPhone, IdentityConstant.DRIVER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key,numberCode+"",2, TimeUnit.MINUTES);

        return ResponseResult.success("");
    }

    /**
     * 司机校验验证码
     * @param driverPhone
     * @param verificationCode
     * @return
     */
    public  ResponseResult checkCode(String driverPhone,String verificationCode){
        //根据手机号去redis读取验证码
        String key = RedisPrefixUtils.generatorKeyByPhone(driverPhone,IdentityConstant.DRIVER_IDENTITY);
        String codeRedis = stringRedisTemplate.opsForValue().get(key);
        System.out.println(codeRedis+"....");

        //校验验证码
        //已经过期
        if(StringUtils.isEmpty(codeRedis)){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_EXPIRE.getCode(),
                    CommonStatusEnum.VERIFICATION_CODE_EXPIRE.getValue());
        }
        //不正确
        if(!verificationCode.trim().equals(codeRedis.trim())){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),
                    CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }


        //颁发令牌
        String accessToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenConstants.REFRESH_TOKEN_TYPE);

        //将token存在redis
        String  accessTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone,IdentityConstant.DRIVER_IDENTITY,TokenConstants.ACCESS_TOKEN_TYPE);


        String  refreshTokenKey = RedisPrefixUtils.generatorTokenKey(driverPhone,IdentityConstant.DRIVER_IDENTITY,TokenConstants.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);


        //响应
        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);
        return ResponseResult.success(tokenResponse);
    }

}
